package io;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClient01 {
    public static void main(String[] args) throws IOException {
        //创建HtttpClient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            //创建请求
            HttpGet request = new HttpGet("http://localhost:8801");
            //获取响应
            CloseableHttpResponse response = httpClient.execute(request);
            try {
                //取得响应信息
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    //取得响应内容
                    String result = EntityUtils.toString(entity);
                    System.out.println(result);
                }
            } finally {
                //关闭响应
                response.close();
            }
        } finally {
            //关闭客户商端
            httpClient.close();
        }
    }
}